package org.groupg.practice;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class AsyncRequestExample {
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url("https://api.example.com/data")
            .build();

        client.newCall(request).enqueue(new Callback() { // 异步执行
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace(); // 处理请求失败
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    System.out.println("Response: " + responseBody); // 处理响应
                    // 注意：此回调在后台线程，更新 UI 需切回主线程
                } else {
                    System.out.println("Request failed: " + response.code());
                }
            }
        });
    }
}